package com.pswv2.backstage.territory.action;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pswv2.backstage.category.servic.CategoryService;
import com.pswv2.backstage.territory.servic.TerritoryService;
import com.pswv2.bub.bsc.dao.po.Category;
import com.pswv2.bub.bsc.dao.po.Territory;
import com.pswv2.bub.bsc.dao.po.UserTerritory;
import com.pswv2.util.BackendErrorCode;
import com.pswv2.util.CommonResp;
import com.pswv2.util.ExtraSpringHibernateTemplate;
import com.pswv2.util.Paging;
import com.pswv2.util.Strings;
/**
 * 分类操作
 * @author linjiang
 *
 */
@Controller
@RequestMapping(value = "territoryAction", produces = { "text/json;charset=UTF-8" })
public class TerritoryAction {
	private static Class<?> poc = Territory.class;
	private static Class<?> pocUT = UserTerritory.class;
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;
	@Autowired
	private TerritoryService territoryService;
	
	
	/**
	 * 查询用户通过用户Id所在类返回所有类加用户的所在的领域
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = { "queryUserTerritoryById" })
	public String queryUserTerritoryById(int userId) {
		List<Territory> ls=(List<Territory>) extraSpringHibernateTemplate.getHibernateTemplate().find("select t FROM UserTerritory ut , Territory t where ut.territoryId=t.territoryId AND ut.userId=?", userId);
		return CommonResp.buildSuccessResp(ls).toJsonString();
	}
	/**
	 * 查询用户所在类返回所有类加用户的所在的领域
	 */
	@ResponseBody
	@RequestMapping(value = { "queryUserTerritory" })
	public String queryUserTerritory(int userId) {
		List<Territory> allTerritory =  territoryService.queryAll();
		List<UserTerritory> userTerritory=extraSpringHibernateTemplate.findAllByPropEq(pocUT, "userId", userId);
		List ls=new ArrayList<>();
		for(Territory t:allTerritory){
			boolean b=false;
			Map<String,Object>map =new HashMap<String,Object>();
			for(UserTerritory ut:userTerritory){
				if(t.getTerritoryId()==ut.getTerritoryId()){
					b=true;
					break;
				}
			}
			if(b){
				map.put("status", 1);	
			}else{
				map.put("status", 0);
			}
			map.put("territory", t);
			ls.add(map);
		}
		return CommonResp.buildSuccessResp(ls).toJsonString();
	}
	/**
	 * 添加
	 */
	@ResponseBody
	@RequestMapping(value = { "addTerritory" })
	public String add(String name) {
		Territory territory = new Territory();
		territory.setName(name);
		territoryService.add(territory);
		return CommonResp.buildSuccessResp().toJsonString();
	}

	@ResponseBody
	@RequestMapping(value = { "listAll" })
	public String list(HttpSession session) {
		List<Territory> allTerritory =  territoryService.queryAll();
		return CommonResp.buildSuccessResp(allTerritory).toJsonString();
	}
	/**
	 * 查看所有
	 */
	 
	@ResponseBody
	@RequestMapping(value = { "list" })
	public String list(int pageIndex, int pageSize, HttpSession session) {
		pageIndex=pageIndex*pageSize;
		Paging<?> paging = extraSpringHibernateTemplate.findPage(poc, Order.desc("territoryId"),pageIndex, pageSize);
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}

	/**
	 * 通过Id查询
	 */
	
	@ResponseBody
	@RequestMapping(value = { "queryById" })
	public String queryById(int id) {
		Territory territory = territoryService.queryById(id);
		if (territory == null) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
		return CommonResp.buildSuccessResp(territory).toJsonString();
	}
	
}
